/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.example.zhpan.bannerviewpager.ResourceTable;
import com.example.zhpan.bannerviewpager.adapter.MainPagerAdapter;
import com.example.zhpan.bannerviewpager.adapter.Untils;
import com.example.zhpan.bannerviewpager.adapter.ViewCreateHelper;

import com.zhpan.bannerview.Log;
import com.zhpan.bannerview.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;

/**
 * MainBannerAbilitySlice
 *
 * @since 2021-03-24
 */
public class MainBannerAbilitySlice extends AbilitySlice
    implements TabList.TabSelectedListener,
    PageSlider.PageChangedListener,
    OnBannerListener {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainBannerAbilitySlice");
    private static final int ZERO = 0;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private static final int THREE = 3;
    private static final int FOUR = 4;
    Integer[] tabsImage1 = {ResourceTable.Media_home_hover,
        ResourceTable.Media_creative_none,
        ResourceTable.Media_discover_none,
        ResourceTable.Media_mine_none};
    Integer[] tabsImage2 = {ResourceTable.Media_home_hover,
        ResourceTable.Media_creative_hover,
        ResourceTable.Media_discover_hover,
        ResourceTable.Media_mine_hover};
    Integer[] tabsImage3 = {ResourceTable.Media_home_none,
        ResourceTable.Media_creative_none,
        ResourceTable.Media_discover_none,
        ResourceTable.Media_mine_none};
    private PageSlider pageSlider;
    private TabList tabList;

    /**
     * onStart
     *
     * @param intent 參數
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_banner);
        HiLog.warn(new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainBannerAbilitySlice"),"some%s","ssss");
        initView();
        initData();
    }

    private void initData() {
        try {
            String[] tabsTitle = getResourceManager().getElement(ResourceTable.Strarray_tab_title).getStringArray();
            for (int i1 = 0; i1 < tabsTitle.length; i1++) {
                TabList.Tab tab = tabList.new Tab(getContext());
                tab.setText(tabsTitle[i1]);
                tab.setAroundElements(null, Untils.getBitmap(tabsImage1[i1], getContext()), null, null);
                tabList.addTab(tab);
            }
            tabList.selectTabAt(0);

            pageSlider.setProvider(new MainPagerAdapter(initPageSliderViewData()));
            pageSlider.setCurrentPage(0);
            pageSlider.setReboundEffect(true);
            pageSlider.setCentralScrollMode(true);
        } catch (IOException | NotExistException | WrongTypeException e) {
            Log.debug(e.getMessage());
        }
    }

    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper(getContext(), MainBannerAbilitySlice.this);
        for (int i1 = 0; i1 < pageSize; i1++) {
            if (ZERO == i1) {
                pages.add(viewCreateHelper.createViewHome(tabList.getTabAt(i1).getText()));
            }
            if (ONE == i1) {
                pages.add(viewCreateHelper.createViewPage(tabList.getTabAt(i1).getText()));
            }
            if (TWO == i1) {
                pages.add(viewCreateHelper.createViewIndicator(tabList.getTabAt(i1).getText()));
            }
            if (THREE == i1) {
                pages.add(viewCreateHelper.createViewOthers(tabList.getTabAt(i1).getText()));
            }
        }
        return pages;
    }

    /**
     * initView
     */
    private void initView() {
        tabList = (TabList) findComponentById(ResourceTable.Id_title_tabs);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);
        tabList.addTabSelectedListener(this);
        pageSlider.addPageChangedListener(this);
    }

    /**
     * onPageSliding
     *
     * @param i2 參數
     * @param v1 參數
     * @param i1 參數
     */
    @Override
    public void onPageSliding(int i2, float v1, int i1) {
    }

    /**
     * onPageSlideStateChanged
     *
     * @param i1 參數
     */
    @Override
    public void onPageSlideStateChanged(int i1) {
    }

    /**
     * onPageChosen
     *
     * @param i1 參數
     */
    @Override
    public void onPageChosen(int i1) {
        if (tabList.getSelectedTab().getPosition() != i1) {
            tabList.selectTabAt(i1);
        }
    }

    /**
     * onSelected
     *
     * @param tab 參數
     */
    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
        if (tab.getPosition() == 0) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage2[0], getContext()), null, null);
        } else if (tab.getPosition() == 1) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage2[1], getContext()), null, null);
        } else if (tab.getPosition() == TWO) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage2[TWO], getContext()), null, null);
        } else if (tab.getPosition() == THREE) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage2[THREE], getContext()), null, null);
        }
    }

    /**
     * onUnselected
     *
     * @param tab 參數
     */
    @Override
    public void onUnselected(TabList.Tab tab) {
        if (tab.getPosition() == 0) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage3[0], getContext()), null, null);
        } else if (tab.getPosition() == 1) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage3[1], getContext()), null, null);
        } else if (tab.getPosition() == TWO) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage3[TWO], getContext()), null, null);
        } else if (tab.getPosition() == THREE) {
            tab.setAroundElements(null, Untils.getBitmap(tabsImage3[THREE], getContext()), null, null);
        }
    }

    /**
     * onReselected
     *
     * @param tab 參數
     */
    @Override
    public void onReselected(TabList.Tab tab) {
    }

    /**
     * onBannerClick
     *
     * @param position 參數
     */
    @Override
    public void onBannerClick(int position) {
        if (position == THREE) {
            present(new SecondAbilitySlice(), new Intent());
        }
        if (position == 0) {
            present(new ForthAbilitySlice(), new Intent());
        }
        if (position == TWO) {
            present(new FiveAbilitySlice(), new Intent());
        }
    }
}